package meijuli.com.join51.business.location;

import android.content.Context;
import android.graphics.Color;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import meijuli.com.join51.business.R;

/**
 *
 * @author jack 2017-05-22 16:17
 */
public class LocationPresenterImpl extends Contract.LocationPresenter implements
        PoiSearch.OnPoiSearchListener,
        AMapLocationListener,
        LocationSource{
    private AMapLocationClient        mlocationClient;
    private OnLocationChangedListener mListener;
    private AMapLocationClientOption  mLocationOption;
    private Context                   mContext;

    @Override
    public void searchNearby(double latitude, double longitude, String keyword, int pageIndex) {
        PoiSearch.Query query = new PoiSearch.Query(keyword, "", "");
        query.setPageNum(pageIndex);
        query.setPageSize(20);
        try {
            PoiSearch poiSearch = new PoiSearch(mContext, query);
            poiSearch.setOnPoiSearchListener(this);
            poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 2000, true));
            poiSearch.searchPOIAsyn();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initLocation(AMap aMap) {
        if(aMap == null || mView == null) {
            return;
        }
        mView.showLoading();
        // 自定义系统定位小蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 设置小蓝点的图标
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory
                .fromResource(R.mipmap.location_marker));
        // 设置圆形的边框颜色
        myLocationStyle.strokeColor(Color.BLACK);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180));
        // 设置圆形的边框粗细
        myLocationStyle.strokeWidth(1.0f);
        aMap.setMyLocationStyle(myLocationStyle);
        // 设置定位监听
        aMap.setLocationSource(this);
        // 缩放级别3-19，不支持自定义
        aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
        // 设置默认定位按钮是否显示
        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        // 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        aMap.setMyLocationEnabled(true);
    }

    @Override
    void setContext(Context context) {
        mContext = context;
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        if(poiResult == null || mView == null) {
            return;
        }
        mView.setData(poiResult.getPois());
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(mContext);
            //初始化定位参数
            mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mlocationClient.setLocationListener(this);
            //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置是否只定位一次,默认为false
            mLocationOption.setOnceLocation(true);
            //设置定位间隔,单位毫秒,默认为2000ms
            mLocationOption.setInterval(2000);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            startLocation();
        }
    }

    public void startLocation() {
        //启动定位
        mlocationClient.startLocation();
    }

    @Override
    public void deactivate() {
        mListener = null;
        if(mView != null) {
            mView.hideLoading();
        }
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if(aMapLocation == null || mView == null) {
            return;
        }
        mView.hideLoading();
        mListener.onLocationChanged(aMapLocation);
        mView.locationResult(aMapLocation);
    }

    @Override
    public void recycler() {

    }
}
